<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-02 19:42:06
 * @LastEditTime: 2023-06-28 13:49:49
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Repositories;

use App\Models\MarketOfferPromotions;
use App\Models\ProColorMy;
use Illuminate\Database\Eloquent\Model;

/**
 * 报价
 * @property MarketOfferPromotions $model 
 * @author sunkaiyuan
 */
class MarketOfferPromotionsRepository extends Repositories
    {
    protected $relation_models = ['customer', 'department', 'customer', 'service', 'status', 'creator', 'updator', 'auditor'];

    public function boot(MarketOfferPromotions $model, )
        {
        $this->model = $model;
        }

    /**
     *  匹配优惠卷
     * @param int $service_id
     * @param mixed $storage
     * @param \Illuminate\Database\Eloquent\Model $color
     * @param \Illuminate\Database\Eloquent\Model $customer
     * @param mixed $quantity
     * @param mixed $department_id
     * @return mixed
     * @author sunkaiyuan
     */
    public function mate(int $service_id, $grad, $material_id, $color, Model $customer, $quantity, $department_id = null)
        {
        $promos = $this->whereService_id($service_id)
            ->where(function ($query) use ($department_id)
                {
                $query->orWhere("department_id", "=", "0");
                $query->orWhere("department_id", "=", $department_id ?? auth()->user()->department_id);
                })
            ->where(function ($query) use ($customer)
                {
                $query->orWhere("customer_id", "=", "0");
                $query->orWhere("customer_id", "=", $customer['id']);
                })
            ->where("quantity", "<>", 0) //次数是有的，-1  是无限次数
            ->where("status_id", "=", 0) //启用的
            ->where("billdate", "<=", date("Y-m-d H:i:s")) //在日期范围的
            ->get();

        return $promos->filter(function ($promotion) use ($grad, $material_id, $color, $quantity, $customer)
            {
            return $promotion->roles->filter(function ($role) use ($grad, $material_id, $color, $quantity, $customer)
                {
                //构成操作，匹配的值...
                [$action, $value, $defvalue] = $this->mateData($role, $grad, $material_id, $color, $quantity, $customer);
                //匹配失败的返回
                return !$this->mateAction($defvalue, $action, $value);
                })
                ->count() == 0; //匹配失败的大于0 就无法匹配优惠
            })
            ->sortBy("price"); //匹配优惠金额最大的一个
        }

    private function mateData($role, $grad, $material_id, $color, $quantity, $customer)
        {
        $defvalue = null;
        $action   = $role->action;
        $value    = $role->value;
        if (in_array($role->field, ['库存等级'])) {
            $defvalue = $grad ?? "";
            }
        else if (in_array($role->field, ["门市客"])) {
            $action   = "等于";
            $defvalue = $role->action == '等于' ? 1 : 0;
            $value    = $customer->deptcust_id;
            }
        else if (in_array($role->field, ["染厂色号"])) {
            $defvalue = $color?->code;
            }
        else if (in_array($role->field, ["染厂色名"])) {
            $defvalue = $color?->name;
            }
        else if (in_array($role->field, ["起订量"])) {
            $defvalue = $quantity;
            }
        else if (in_array($role->field, ["色卡"])) {
            $value    = $role->action == '等于' ? 1 : 0;
            $defvalue = ProColorMy::whereMaterial_id($material_id)->whereColor_id($color->id ?? 0)->first()?->ismy ?? 0;
            $action   = "等于";
            }
        return [$action, $value, $defvalue];
        }

    private function mateAction($defvalue, $action, $value)
        {
        if ($action == '等于') {
            return $defvalue == $value;
            }
        if ($action == '不等于') {
            return $defvalue != $value;
            }
        if ($action == '大于') {
            return $defvalue > $value;
            }
        if ($action == '大于等于') {
            return $defvalue >= $value;
            }
        if ($action == '小于') {
            return $defvalue < $value;
            }
        if ($action == '小于等于') {
            return $defvalue <= $value;
            }
        if ($action == '包含') {
            return $this->isinclude($defvalue, $value);
            }

        if ($action == '不包含') {
            return !$this->isinclude($defvalue, $value);
            }
        }
    private function isinclude($defvalue, $value)
        {
        $_v = explode(",", $value);
        if (count($_v) == 1) {
            return stripos($defvalue, $value) >= 0;
            }
        else {
            return in_array($defvalue, $_v);
            }
        }
    }